package com.demo.core.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.util.Assert;

import com.demo.core.cache.CacheKey;
import com.demo.core.cache.CacheWrapper;
import com.demo.core.dao.MenuDao;
import com.demo.core.dao.impl.base.PageCoreDaoSupport;
import com.demo.core.model.Menu;
import com.demo.core.model.support.GeneralExecuteModel;

/**
 * MENU数据库访问层实现类
 * 
 * @author ryuu.kk
 */
public class MenuDaoImpl extends PageCoreDaoSupport<Menu, Long> implements MenuDao {
	/**
	 * MENU空间 
	 */
	protected final static String FOLDER_SPACE = "menuSpace.";
	/**
	 * 模型表名
	 */
	protected final static String TABLE_NAME = "T_CORE_MENU";
	
	/**
	 * 模型表主键ID
	 */
	protected final static String TABLE_PK_NAME = "ID";
	
	@Override
	public int enabled(Menu t, Map<String, Object> params) {
		Assert.notNull(t);
		Assert.notNull(params);
		params.put(getModelName(t), t);
		if (LOG.isDebugEnabled()) {
			LOG.debug("execute update method, mode is " + t.getClass().getSimpleName());
		}
		cacheClear();
		
		return getSqlMapClientTemplate().update(nameSpace() + "updateEnabled", params);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Menu> tree(Map<String, Object> params) {
		CacheKey key = null;
		List<Menu> result = null;
		CacheWrapper<CacheKey, Object> cache = null;
		
		if (isUseCache() && (cache = getCache()) != null) {
			key = new CacheKey();
			key.update("tree");
			key.update(params.hashCode());
			result = (List<Menu>) cache.get(key);
		}
		if (result == null) {
			result  = getSqlMapClientTemplate().queryForList(FOLDER_SPACE + "queryTree", params);
			if (isUseCache() && cache != null && result != null) {
				cache.put(key, result);
			}
		}
		return result;
	}

	@Override
	protected String nameSpace() {
		return FOLDER_SPACE;
	}
	@Override
	protected void doInit(GeneralExecuteModel<Long> model) {
		model.setPk(TABLE_PK_NAME);
		model.setTable(TABLE_NAME);
	}
}
